<?php
/**
 * 易优CMS
 * ============================================================================
 * 版权所有 2016-2028 海南赞赞网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.eyoucms.com
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 陈风任 <491085389@qq.com>
 * Date: 2022-03-23
 */

namespace app\plugins\controller;

use think\Db;
use think\Page;
use app\plugins\logic\MultiMerchantLogic;

class MultiMerchant extends Base
{
    // 插件模板标识
    private $Code = 'multimerchant/';

    /**
     * 构造方法
     */
    public function __construct() {
        parent::__construct();
        // 商家主表
        $this->multiMerchantDb = Db::name('WeappMultiMerchant');

        // 商家模型层
        $this->multiMerchantModel = model('MultiMerchant');

        // 商家逻辑层
        $this->multiMerchantLogic = new MultiMerchantLogic($this->users);

        // 商家信息
        $this->merchant_id = 0;
        $this->multiMerchant = [];
        // 是否已登录，未登录则跳转到登录页
        if (!empty($this->users) && !empty($this->users_id)) {
            // 已登录，查询商家信息执行不同操作
            $this->multiMerchantAction();
        } else {
            // 未登录，跳转到登录页
            if (IS_AJAX) {
                $this->error('请先登录', url('user/Users/login'), ['url' => url('user/Users/login')]);
            } else {
                $this->redirect(url('user/Users/login'));
                exit;
            }
        }
        $this->assign('users', $this->users);
        $this->assign('multiMerchant', $this->multiMerchant);
    }

    // 商家申请注册
    public function apply()
    {
        $default_pic = ROOT_DIR . '/public/static/common/images/not_adv.jpg';

        if (IS_AJAX_POST) {
            // 获取提交的参数
            $post = input('post.');

            // 调用逻辑业务层处理
            $this->multiMerchantLogic->multiMerchantSubmitApply($post);
        }

        $this->assign('default_pic', $default_pic);
        return $this->fetch($this->Code . THEME_STYLE . '/apply');
    }

    // 商家登录
    public function login()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');
            if (!empty($post['merchant_pass']) && 6 === strlen(intval($post['merchant_pass']))) {
                $this->multiMerchantLogic->multiMerchantSubmitLogin($post);
            }
            $this->error('请输入6位商家独立密码');
        }
        return $this->fetch($this->Code . THEME_STYLE . '/login');
    }

    // 商家退出
    public function login_quit()
    {
        session('merchantPass', null);
        if (!empty($this->users)) {
            $this->redirect(url('user/Users/index'));
        } else {
            $this->redirect(url('home/Index/index'));
        }
    }

    // 商家概况信息页
    public function overview()
    {
        // 商家概况信息
        $overview = $this->multiMerchantModel->getMerchantOverview($this->merchant_id);
        $this->assign($overview);
        // dump($overview);exit;
        return $this->fetch($this->Code . THEME_STYLE . '/overview');
    }

    // 商家信息首页
    public function index()
    {
        if (IS_AJAX_POST) {
            // 获取提交的参数
            $post = input('post.');

            // 调用逻辑业务层处理
            $this->multiMerchantLogic->multiMerchantSubmitUpdate($post);
        }

        return $this->fetch($this->Code . THEME_STYLE . '/index');
    }

    // 商家联系地址
    public function contact()
    {
        if (IS_AJAX_POST) {
            // 获取提交的参数
            $post = input('post.');

            // 调用逻辑业务层处理
            $this->multiMerchantLogic->multiMerchantSubmitContact($post, $this->merchant_id);
        }

        // 省份
        $this->assign('provinceList', get_province_list());
        $contactCity = [];
        if (!empty($this->multiMerchant['merchant_contact']['contactProvince'])) {
            $contactCity = Db::name('region')->where('parent_id', $this->multiMerchant['merchant_contact']['contactProvince'])->select();
        }
        $this->assign('contactCity', $contactCity);
        $contactDistrict = [];
        if (!empty($this->multiMerchant['merchant_contact']['contactCity'])) {
            $contactDistrict = Db::name('region')->where('parent_id', $this->multiMerchant['merchant_contact']['contactCity'])->select();
        }
        $this->assign('contactDistrict', $contactDistrict);
        return $this->fetch($this->Code . THEME_STYLE . '/contact');
    }

    // 商家认证申请信息
    public function cert()
    {
        if (IS_AJAX_POST) {
            // 获取提交的参数
            $post = input('post.');

            // 调用逻辑业务层处理
            $this->multiMerchantLogic->multiMerchantSubmitApplyCert($post, $this->merchant_id, false);
        }

        // 省份
        $this->assign('provinceList', get_province_list());
        return $this->fetch($this->Code . THEME_STYLE . '/cert');
    }

    // 商家认证编辑信息
    public function cert_edit()
    {
        if (IS_AJAX_POST) {
            // 获取提交的参数
            $post = input('post.');

            // 调用逻辑业务层处理
            $this->multiMerchantLogic->multiMerchantSubmitApplyCert($post, $this->merchant_id, true);
        }

        // 省份
        $this->assign('provinceList', get_province_list());

        // 企业商家地址
        if (!empty($this->multiMerchant['apply_data']['company'])) {
            // 公司信息地址
            $companyCity = [];
            if (!empty($this->multiMerchant['apply_data']['company']['companyProvince'])) {
                $companyCity = Db::name('region')->where('parent_id', $this->multiMerchant['apply_data']['company']['companyProvince'])->select();
            }
            $this->assign('companyCity', $companyCity);
            $companyDistrict = [];
            if (!empty($this->multiMerchant['apply_data']['company']['companyCity'])) {
                $companyDistrict = Db::name('region')->where('parent_id', $this->multiMerchant['apply_data']['company']['companyCity'])->select();
            }
            $this->assign('companyDistrict', $companyDistrict);
            // 对公账号地址
            $openBankCity = [];
            if (!empty($this->multiMerchant['apply_data']['company']['openBankProvince'])) {
                $openBankCity = Db::name('region')->where('parent_id', $this->multiMerchant['apply_data']['company']['openBankProvince'])->select();
            }
            $this->assign('openBankCity', $openBankCity);
            $openBankDistrict = [];
            if (!empty($this->multiMerchant['apply_data']['company']['openBankCity'])) {
                $openBankDistrict = Db::name('region')->where('parent_id', $this->multiMerchant['apply_data']['company']['openBankCity'])->select();
            }
            $this->assign('openBankDistrict', $openBankDistrict);
        }

        // 结算开户行地址
        $bankCity = Db::name('region')->where('parent_id', $this->multiMerchant['settle_data']['bankProvince'])->select();
        $this->assign('bankCity', $bankCity);
        $bankDistrict = Db::name('region')->where('parent_id', $this->multiMerchant['settle_data']['bankCity'])->select();
        $this->assign('bankDistrict', $bankDistrict);

        // dump($this->multiMerchant);exit;
        return $this->fetch($this->Code . THEME_STYLE . '/cert_edit');
    }

    // 联动地址获取
    public function linkage_address()
    {
        $parent_id  = input('param.parent_id/d');
        $RegionData = Db::name('region')->where("parent_id", $parent_id)->select();
        $html = '';
        if (!empty($RegionData)) {

            // 拼装下拉选项
            foreach ($RegionData as $value) {
                $html .= "<option value='{$value['id']}'>{$value['name']}</option>";
            }
        }
        echo json_encode($html);
    }

    // 商品存在异常商品(违规下架、审核失败)
    public function product_abnormal()
    {

        return $this->fetch($this->Code . THEME_STYLE . '/product_abnormal');
    }

    // 商家商品列表
    public function product()
    {
        // 获取商家商品列表
        $param = input('param.');
        $productAssign = $this->multiMerchantModel->getMerchantProductList($this->merchant_id, $param);
        $this->assign($productAssign);

        // 商品栏目
        $arctype_html = allow_release_arctype(input('typeid/d', 0), [2]);
        $this->assign('arctype_html', $arctype_html);
        return $this->fetch($this->Code . THEME_STYLE . '/product');
    }

    // 删除商家商品(真实删除)
    public function product_del()
    {
        if (IS_AJAX_POST) {
            // 商家商品添加
            $aid = input('post.aid/a');
            // 执行删除商品及其关联数据
            $this->multiMerchantLogic->delMerchantProduct($this->merchant_id, $aid);
        }
    }

    // 更新商品上架/下架
    public function product_update()
    {
        if (IS_AJAX_POST) {
            // 商家商品添加
            $post = input('post.');
            // 执行更新商品上架/下架
            $this->multiMerchantLogic->updateMerchantProduct($this->merchant_id, $post);
        }
    }

    // 商家商品添加
    public function product_add()
    {
        if (IS_AJAX_POST) {
            // 商家商品添加
            $post = input('post.');
            // 执行添加商品
            $this->multiMerchantLogic->addMerchantProduct($this->merchant_id, $post);
        }

        // 清除商家自身的规格缓存
        $merchantSpecArray = session($this->merchant_id . '_MerchantSpecArray');
        if (!empty($merchantSpecArray)) {
            // 查询如果存在商家自定义规格则清除
            $presetMarkIds = array_keys($merchantSpecArray);
            $where = [
                'product_add' => 1,
                'merchant_id' => $this->merchant_id,
                'preset_mark_id' => ['IN', $presetMarkIds]
            ];
            Db::name('product_spec_preset')->where($where)->delete();
            // 清除 session
            session($this->merchant_id . '_MerchantSpecArray', null);
        }

        // 可发布商品的栏目
        $typeid = 0;
        $arctypeHtml = allow_release_arctype($typeid, [2], true);
        $arctypeHtml = str_ireplace('data-current_channel=', 'data-channel=', $arctypeHtml);
        $arctypeHtml = "<select name='typeid' id='typeid'><option value='0'>请选择栏目…</option>{$arctypeHtml}</select>";
        $this->assign('arctypeHtml', $arctypeHtml);

        // 查询预设的规格数据
        $specPreset = $this->multiMerchantModel->getProductSpecPreset($this->merchant_id);
        $this->assign('specPreset', $specPreset);

        // 读取规格预设库最大参数标记ID
        $maxPresetMarkID = $this->multiMerchantModel->getMaxPresetMarkID();
        $this->assign('maxPresetMarkID', $maxPresetMarkID);

        return $this->fetch($this->Code . THEME_STYLE . '/product_add');
    }

    // 选中规格名称，追加html到页面展示
    public function product_spec_name()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');
            // 读取session信息
            $MerchantSpecArray = session($this->merchant_id . '_MerchantSpecArray');
            $MerchantSpecArray = !empty($MerchantSpecArray) ? count($MerchantSpecArray) : [];

            // 当选中的规格名称超过三个，不允许再添加
            if (3 === intval($MerchantSpecArray)) $this->error('最多使用三种商品规格大类');

            // 获取预设规格标记ID数组
            $usePresetMarkID = $this->multiMerchantLogic->getUsePresetMarkID($post);

            // 拼装预设名称下拉选项
            if (!empty($usePresetMarkID)) {
                // 剔除已选择的规格名称查询未选择的规格名称组装成下拉返回
                $nameOption = $this->multiMerchantLogic->getPresetNameOption($this->merchant_id, $usePresetMarkID);
            } else {
                $this->error('最多使用三种商品规格大类');
            }

            // 拼装预设规格名称的规格值下拉选项
            $valueOption = $this->multiMerchantLogic->getPresetValueOption($this->merchant_id, $post['presetMarkID']);

            // 结果返回
            $ReturnHtml = [
                'presetName' => $valueOption['name'],
                'usePresetMarkID' => $nameOption['markID'],
                'presetNameOption' => $nameOption['option'],
                'presetValueOption' => $valueOption['option'],
            ];
            $this->success('加载成功！', null, $ReturnHtml);
        }
    }

    // 追加预设规格名称和规格值
    public function product_spec_value()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');

            // 整合Session和Post中的规格数据
            $SpecArray = $this->multiMerchantLogic->getMergeSessionAndPostArray($this->merchant_id, $post);

            // 获取已选规格进行HTML代码拼装
            $htmlTable = $this->multiMerchantLogic->getProductSpecAssembly($SpecArray);

            // 剔除已选择的规格值查询未选择的规格值组装成下拉返回
            $valueOption = $this->multiMerchantLogic->getNotInPresetValueOption($this->merchant_id, $post);

            // 返回数据
            $ReturnData = [
                'htmlTable' => $htmlTable,
                'presetValue' => $valueOption['value'],
                'presetValueOption' => $valueOption['option'],
            ];
            $this->success('加载成功！', null, $ReturnData);
        }
    }

    // 保存新增规格并使用
    public function product_spec_save_use()
    {
        if (IS_AJAX_POST) {
            $param = input('param.');

            // 添加自定义规格到规格预设表
            $ResultArray = $this->multiMerchantLogic->addProductSpecSaveUse($this->merchant_id, $param);

            // 获取已选规格进行HTML代码拼装
            $htmlTable = $this->multiMerchantLogic->getProductSpecAssembly($ResultArray['SpecArray']);

            // 返回数据
            $ReturnData = [
                'htmlTable' => $htmlTable,
                'presetMarkID' => $ResultArray['presetMarkID'],
                'usePresetMarkID' => $ResultArray['usePresetMarkID'],
            ];
            $this->success('加载成功！', null, $ReturnData);
        }
    }

    // 清除指定的规格信息(session)
    public function product_spec_preset_clear()
    {
        if (IS_AJAX_POST) {
            $param = input('param.');

            // 清除指定的规格信息(session)
            $ResultArray = $this->multiMerchantLogic->productSpecPresetClear($this->merchant_id, $param);

            // 获取已选规格进行HTML代码拼装
            $htmlTable = $this->multiMerchantLogic->getProductSpecAssembly($ResultArray['SpecArray']);

            // 返回数据
            $ReturnData = [
                'htmlTable' => $htmlTable,
                'usePresetMarkID' => !empty($ResultArray['markID']) ? $ResultArray['markID'] : 0,
                'presetNameOption' => !empty($ResultArray['nameOption']) ? $ResultArray['nameOption'] : '',
                'presetValueOption' => !empty($ResultArray['valueOption']) ? $ResultArray['valueOption'] : ''
            ];
            $this->success('加载成功！', null, $ReturnData);
        }
    }



    // 商品编辑页
    public function product_edit()
    {
        if (IS_AJAX_POST) {
            // 商家商品编辑
            $post = input('post.');
            // 执行添加商品
            $this->multiMerchantLogic->editMerchantProduct($this->merchant_id, $post);
        }

        // 商品ID
        $aid = input('aid/d', 0);
        empty($aid) && $this->error('请选择需要编辑的商品');

        // 查询商品信息
        $productInfo = $this->multiMerchantLogic->getMerchantProductDetails($this->merchant_id, $aid);
        empty($productInfo) && $this->error('选择的商品不存在');
        // dump($productInfo);exit;
        $this->assign($productInfo);

        return $this->fetch($this->Code . THEME_STYLE . '/product_edit');
    }

    // 保存新增规格并使用
    public function product_spec_data_handle_save_use()
    {
        if (IS_AJAX_POST) {
            $param = input('param.');

            // 添加自定义规格到商品规格库
            $ResultArray = $this->multiMerchantLogic->addProductSpecDataHandleSaveUse($this->merchant_id, $param);

            // 获取已选规格进行HTML代码拼装
            $htmlTable = $this->multiMerchantLogic->getProductSpecEditAssembly($ResultArray['specArray'], $param['aid']);

            // 返回数据
            $ReturnData = [
                'htmlTable' => $htmlTable,
                'specMarkID' => !empty($ResultArray['specMarkID']) ? $ResultArray['specMarkID'] : 0,
                'useSpecMarkID' => !empty($ResultArray['useSpecMarkID']) ? $ResultArray['useSpecMarkID'] : '',
            ];
            $this->success('加载成功！', null, $ReturnData);
        }
    }

    // 追加商品规格名称
    public function product_spec_data_handle_name()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');

            // 追加商品规格名称
            $nameOption = $this->multiMerchantLogic->productSpecDataHandleName($this->merchant_id, $post);

            // 剔除已选择的规格值查询未选择的规格值组装成下拉返回
            $valueOption = $this->multiMerchantLogic->getProductSpecDataValueOption($post['aid'], $post['specMarkID']);

            // 结果返回
            $returnHtml = [
                'specName' => $valueOption['name'],
                'useSpecMarkID' => $nameOption['markID'],
                'presetNameOption' => $nameOption['option'],
                'specValueOption' => $valueOption['option'],
            ];
            $this->success('加载成功！', null, $returnHtml);
        }
    }

    // 追加商品规格名称和规格值
    public function product_spec_data_handle_value()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');

            // 追加商品规格名称和规格值
            $specArray = $this->multiMerchantLogic->productSpecDataHandleValue($this->merchant_id, $post);

            // 获取已选规格进行HTML代码拼装
            $htmlTable = $this->multiMerchantLogic->getProductSpecEditAssembly($specArray['SpecArray'], $post['aid']);

            // 返回数据
            $ReturnData = [
                'htmlTable' => $htmlTable,
                'specValue' => !empty($specArray['value']) ? $specArray['value'] : '',
                'specValueOption' => !empty($specArray['valueOption']) ? $specArray['valueOption'] : '',
            ];
            $this->success('加载成功！', null, $ReturnData);
        }
    }

    // 清除商品规格处理表的规格信息
    public function product_spec_data_handle_clear()
    {
        if (IS_AJAX_POST) {
            $param = input('param.');

            // 清除商品规格处理表的规格信息
            $ResultArray = $this->multiMerchantLogic->productSpecDataHandleClear($this->merchant_id, $param);

            // 获取已选规格进行HTML代码拼装
            $htmlTable = $this->multiMerchantLogic->getProductSpecEditAssembly($ResultArray['SpecArray'], $param['aid']);

            // 返回数据
            $ReturnData = [
                'htmlTable' => $htmlTable,
                'useSpecMarkID' => !empty($ResultArray['markID']) ? $ResultArray['markID'] : 0,
                'presetNameOption' => !empty($ResultArray['nameOption']) ? $ResultArray['nameOption'] : '',
                'specValueOption' => !empty($ResultArray['valueOption']) ? $ResultArray['valueOption'] : '',
            ];

            $this->success('加载成功！', null, $ReturnData);
        }
    }

    // 有效订单列表
    public function order()
    {
        // 获取商家订单列表
        $orderAssign = $this->multiMerchantModel->getMerchantOrderList($this->merchant_id);
        $this->assign($orderAssign);

        return $this->fetch($this->Code . THEME_STYLE . '/order');
    }

    // 有效订单详情
    public function order_details()
    {
        // 订单ID
        $order_id = input('param.order_id/d', 0);
        if (!empty($order_id)) {
            // 查询商家订单详情信息
            $orderAssign = $this->multiMerchantModel->getMerchantOrderDetails($this->merchant_id, $order_id);
            $this->assign($orderAssign);
        } else {
            $this->error('非法访问..');
        }

        return $this->fetch($this->Code . THEME_STYLE . '/order_details');
    }

    // 订单关闭
    public function order_close()
    {
        if (IS_AJAX_POST) {
            $order_id = input('post.order_id/d', 0);
            // 关闭商家订单
            $this->multiMerchantLogic->closeMerchantOrder($this->merchant_id, $order_id);
        }
    }

    // 订单确认收货
    public function order_confirm()
    {
        if (IS_AJAX_POST) {
            $order_id = input('post.order_id/d', 0);
            // 确认商家订单收货
            $this->multiMerchantLogic->confirmMerchantOrder($this->merchant_id, $order_id);
        }
    }

    // 订单发货
    public function order_send()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');
            // 商家订单发货
            $this->multiMerchantLogic->sendMerchantOrder($this->merchant_id, $post);
        } else {
            $order_id = input('param.order_id/d', 0);
            if (!empty($order_id)) {
                // 查询订单信息
                $orderAssign = $this->multiMerchantModel->getMerchantOrderDetails($this->merchant_id, $order_id);
                $this->assign($orderAssign);

                $where = [
                    'is_choose' => 1,
                ];
                $express = Db::name('shop_express')->where($where)->order('sort_order asc, express_id asc')->select();
                $this->assign('express', $express);
                return $this->fetch($this->Code . THEME_STYLE . '/order_send');
            }
        }
    }

    // 售后订单列表
    public function service()
    {
        // 获取商家售后订单列表
        $serviceAssign = $this->multiMerchantModel->getMerchantServiceList($this->merchant_id);
        $this->assign($serviceAssign);

        return $this->fetch($this->Code . THEME_STYLE . '/service');
    }

    // 售后订单详情
    public function service_details()
    {
        $service_id = input('param.service_id/d');
        if (!empty($service_id)) {
            // 获取商家售后订单详情
            $serviceAssign = $this->multiMerchantModel->getMerchantServiceDetails($this->merchant_id, $service_id);
            $this->assign($serviceAssign);
            return $this->fetch($this->Code . THEME_STYLE . '/service_details');
        } else {
            $this->error('非法访问..');
        }
    }

    // 售后订单处理
    public function service_deal_with()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');
            // 售后订单处理
            $this->multiMerchantLogic->dealWithMerchantService($this->merchant_id, $this->users_id, $post);
        }
    }

    // 资产概况
    public function asset_overview()
    {
        // 获取商家资产详情
        $assetAssign = $this->multiMerchantModel->getMerchantAssetOverview($this->merchant_id);
        $this->assign($assetAssign);
        return $this->fetch($this->Code . THEME_STYLE . '/asset_overview');
    }

    // 订单明细
    public function asset_order()
    {
        // 获取商家订单列表
        $orderAssign = $this->multiMerchantModel->getMerchantOrderList($this->merchant_id);
        $this->assign($orderAssign);
        return $this->fetch($this->Code . THEME_STYLE . '/asset_order');
    }

    // 提现记录
    public function asset_withdraw()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');
            // 撤销提现订单
            $this->multiMerchantLogic->revokeMerchantAssetWithdraw($this->merchant_id, $post);
        } else {
            // 获取商家提现记录
            $withdrawAssign = $this->multiMerchantModel->getMerchantWithdrawList($this->merchant_id);
            $this->assign($withdrawAssign);
            return $this->fetch($this->Code . THEME_STYLE . '/asset_withdraw');
        }
    }

    // 余额提现内页
    public function asset_withdraw_apply()
    {
        if (IS_AJAX_POST) {
            $withdraw_money = input('withdraw_money/f', 0);
            if (!empty($withdraw_money)) {
                if (floatval($withdraw_money) > floatval($this->multiMerchant['available_balance'])) $this->error('提现金额超过可提现金额..');
                // 商家余额提现申请
                $resultID = $this->multiMerchantModel->addMerchantAssetWithdrawApply($this->multiMerchant, $withdraw_money);
                if (!empty($resultID)) {
                    $this->success('提交成功，等待审核..');
                } else {
                    $this->error('提交失败，刷新重试..');
                }
            } else {
                $this->error('请填写提现金额..');
            }
        } else {
            // 银行卡地址处理
            $bankProvince = get_province_name($this->multiMerchant['settle_data']['bankProvince']);
            $bankCity = get_city_name($this->multiMerchant['settle_data']['bankCity']);
            $bankDistrict = get_area_name($this->multiMerchant['settle_data']['bankDistrict']);
            $this->multiMerchant['settle_data']['address'] = $bankProvince . ' ' . $bankCity . ' ' . $bankDistrict;
            $this->assign('multiMerchant', $this->multiMerchant);
            
            return $this->fetch($this->Code . THEME_STYLE . '/asset_withdraw_apply');
        }
    }

    // 查询商家信息执行不同操作
    private function multiMerchantAction()
    {
        // 查询商家信息
        $Merchant = $this->multiMerchantModel->getFindMerchant($this->users_id, 'users_id');

        if (IS_AJAX) {
            // 待添加逻辑...
        } else {
            // 页面方法变量
            $action = request()->action();

            // 判断商家信息，执行重定向
            if (empty($Merchant) && !in_array($action, ['apply', 'login', 'login_quit'])) {
                // 未注册，让其先注册
                $this->redirect(url('plugins/MultiMerchant/apply'));
                exit;
            } else if (!empty($Merchant) && !in_array($action, ['login', 'login_quit'])) {
                // session记录的商家密码
                $sessionMerchantPass = session('merchantPass');
                // 获取加密后的商家密码
                $merchantPass = $this->multiMerchantLogic->md5MultiMerchantPass($Merchant);
                // 验证是否正确登录，如果匹对不上则重定向到登录页
                if (strval($merchantPass) !== strval($sessionMerchantPass)) {
                    $this->redirect(url('plugins/MultiMerchant/login'));
                    exit;
                } else if (strval($merchantPass) === strval($sessionMerchantPass)) {
                    if (in_array($action, ['apply'])) {
                        if (empty($Merchant['apply_type']) && empty($Merchant['apply_data'])) {
                            $this->redirect(url('plugins/MultiMerchant/cert'));
                            exit;
                        } else if (!empty($Merchant['apply_type']) && !empty($Merchant['apply_data'])) {
                            $this->redirect(url('plugins/MultiMerchant/cert_edit'));
                            exit;
                        }
                    } else if (in_array($action, ['cert']) && !empty($Merchant['apply_type']) && !empty($Merchant['apply_data'])) {
                        $this->redirect(url('plugins/MultiMerchant/cert_edit'));
                        exit;
                    }
                }
            }

            // 当前操作方法
            $this->assign('action', $action);
        }

        // 赋值商家信息
        $this->multiMerchant = !empty($Merchant) ? $Merchant : [];
        $this->merchant_id = !empty($Merchant['merchant_id']) ? $Merchant['merchant_id'] : 0;
    }
}